<?php

namespace common\models\survey;

use Yii;
use common\helpers\Url;

/**
 * This is the model class for table "{{%survey}}".
 *
 * @property int $id
 * @property int $user_id 用户ID
 * @property int $company_id 公司ID
 * @property int $substation_id 变电站ID
 * @property int $voltage_id 电压等级ID
 * @property int $category_id 检查内容ID
 * @property int $c1 检查项目ID
 * @property int $c2 检查大类ID
 * @property int $c3 检查子类ID
 * @property string $position 隐患位置
 * @property string $content 隐患内容
 * @property int $danger_level_id 隐患等级ID
 * @property int $solved 是否治理完成
 * @property int $department_id 治理责任单位
 * @property string $director 负责人
 * @property string $pretreatment 治理前防范措施
 * @property string $solution 治理措施
 * @property int $plan_time 计划治理时间
 * @property int $completed_at 治理完成时间
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class Survey extends \common\db\ActiveRecord {

    /**
     * 状态
     */
    const STATUS_SOLVED = 100;
    const STATUS_ACTIVE = 10;
    const STATUS_DEFAULT = 0;
    const SOLVED_YES = 1;
    const SOLVED_NO = 0;

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%survey}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['user_id', 'company_id', 'substation_id', 'voltage_id', 'category_id', 'c1', 'c2', 'c3', 'danger_level_id', 'solved', 'department_id', 'plan_time', 'completed_at', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['pretreatment', 'solution'], 'string'],
            [['position', 'content'], 'string', 'max' => 255],
            [['director'], 'string', 'max' => 50],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'user_id' => '用户ID',
            'company_id' => '公司ID',
            'substation_id' => '变电站ID',
            'voltage_id' => '电压等级ID',
            'category_id' => '检查内容ID',
            'c1' => '检查项目ID',
            'c2' => '检查大类ID',
            'c3' => '检查子类ID',
            'position' => '隐患位置',
            'content' => '隐患内容',
            'danger_level_id' => '隐患等级ID',
            'solved' => '是否治理完成',
            'department_id' => '治理责任单位',
            'director' => '负责人',
            'pretreatment' => '治理前防范措施',
            'solution' => '治理措施',
            'plan_time' => '计划治理时间',
            'completed_at' => '治理完成时间',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function getReporter() {
        return $this->hasOne(\common\models\user\User::class, ['id' => 'user_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getLogs() {
        return $this->hasMany(SurveyLog::class, ['survey_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getLastLog() {
        return $this->hasOne(SurveyLog::class, ['survey_id' => 'id'])->orderBy([
            'created_at' => SORT_DESC
        ]);
    }

    /**
     * {@inheritdoc}
     */
    public function getUpdater() {
        return $this->hasOne(\common\models\user\User::class, ['id' => 'user_id'])->via('lastLog');
    }

    /**
     * {@inheritdoc}
     */
    public function getCompany() {
        return $this->hasOne(Company::class, ['id' => 'company_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCityCompany() {
        return $this->hasOne(Company::class, ['id' => 'parent_id'])->via('company');
    }

    /**
     * {@inheritdoc}
     */
    public function getSubstation() {
        return $this->hasOne(Substation::class, ['id' => 'substation_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getVoltage() {
        return $this->hasOne(Voltage::class, ['id' => 'voltage_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getCategory() {
        return $this->hasOne(\common\models\work\Category::class, ['id' => 'category_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getProgram() {
        return $this->hasOne(\common\models\work\Category::class, ['id' => 'c1']);
    }

    /**
     * {@inheritdoc}
     */
    public function getDepartment() {
        return $this->hasOne(\common\models\work\CompanyDepartment::class, ['id' => 'department_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getMasterType() {
        return $this->hasOne(\common\models\work\Category::class, ['id' => 'c2']);
    }

    /**
     * {@inheritdoc}
     */
    public function getSubType() {
        return $this->hasOne(\common\models\work\Category::class, ['id' => 'c3']);
    }

    /**
     * {@inheritdoc}
     */
    public function getDangerLevel() {
        return $this->hasOne(DangerLevel::class, ['id' => 'danger_level_id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getAttachs() {
        return $this->hasMany(SurveyAttach::class, ['survey_id' => 'id']);
    }

    /**
     * {@inheritdoc}
     */
    public function getSolvedStatus() {
        return $this->solved ? '已治理' : '未治理';
    }

    /**
     * {@inheritdoc}
     */
    public function getThumb() {
        $media = $this->getAttachs()->andWhere([
            'type' => 0,
            'format' => 0,
        ])->one();
        if (!$media) {
            return '';
        }
        return $media->thumb;
    }

    /**
     * {@inheritdoc}
     */
    public function getProblemMedias() {
        $medias = $this->getAttachs()->andWhere([
            'type' => 0,
        ])->select('path')->column();

        array_walk($medias, function(&$item) {
            $item = Url::attach($item);
        });
        return implode("、", $medias);
    }

    /**
     * {@inheritdoc}
     */
    public function getSolutionMedias() {
        $medias = $this->getAttachs()->andWhere([
            'type' => 1,
        ])->select('path')->column();
        array_walk($medias, function(&$item) {
            $item = Url::attach($item);
        });
        return implode("、", $medias);
    }

    /**
     * {@inheritdoc}
     */
    public function recordLog($user, $mark = null) {
        $log = new SurveyLog();
        $log->setAttributes([
            'survey_id' => $this->id,
            'user_id' => $user ? $user->id : 0,
            'admin_id' => Yii::$app->user->id,
            'company_id' => 0,
            'name' => '',
            'status' => SurveyLog::STATUS_ACTIVE,
        ]);
        if ($user) {
            $log->company_id = $user->company_id;
            $log->name = $user->realname;
        } else {
            $adminer = Yii::$app->user->identity;
            $log->company_id = $adminer->company_id;
            $log->name = $adminer->username;
        }
        if ($mark) {
            $log->mark = $mark;
        }
        if (!$log->save()) {
            throw new \yii\db\Exception($log->getErrorMsg());
        }
    }

}
